//
//  SearchSortView.swift
//  Juejin App Demo
//
//  Created by 葛鹏 on 2021/8/15.
//

import SwiftUI

struct SearchSortView: View {
    
    @State var sortViewIsExpanded: Bool
    
    @State var articleSort: Int = 0
    @State var timeSort: Int = 0
    
    @State var isTimeSort: Bool = false
    
    var body: some View {
        HStack{
            
            DisclosureGroup(
                isExpanded: $sortViewIsExpanded,
                content: {
                    VStack{
                        Divider().padding(.vertical)
                        if isTimeSort {
                            ForEach(0..<Constants.timeSorts.count){index in
                                HStack{
                                    Text(Constants.timeSorts[index]).padding(.horizontal).padding(.vertical, 5)
                                    Spacer()
                                    if index == timeSort {
                                        Image(systemName: "checkmark").padding(.horizontal).padding(.vertical, 5).foregroundColor(.blue)
                                    }
                                }.onTapGesture {
                                    withAnimation{
                                        timeSort = index
                                        sortViewIsExpanded = false
                                    }
                                    
                                }
                            }
                        } else {
                            ForEach(0..<Constants.articleSorts.count){index in
                                HStack{
                                    Text(Constants.articleSorts[index]).padding(.horizontal).padding(.vertical, 5)
                                    
                                   
                                    Spacer()
                                    if index == articleSort {
                                        Image(systemName: "checkmark").padding(.horizontal).padding(.vertical, 5).foregroundColor(.blue)
                                    }
                                }.onTapGesture {
                                    withAnimation{
                                        articleSort = index
                                        sortViewIsExpanded = false
                                    }
                                   
                                }
                                
                            }
                        }
                        
                        
                    }
                },
                label: { HStack{
                    Spacer()
                    HStack{
                        Text(Constants.articleSorts[articleSort])
                        Image(systemName: "chevron.down")
                    }.onTapGesture {
                        if !isTimeSort {
                            sortViewIsExpanded.toggle()
                        }
                        
                        isTimeSort = false
                    }
                    Spacer()
                    HStack{
                        Text(Constants.timeSorts[timeSort])
                        Image(systemName: "chevron.down")
                    }.onTapGesture {
                        if isTimeSort {
                            sortViewIsExpanded.toggle()
                        }
                        
                        isTimeSort = true
                    }
                    Spacer()
                } }
            ).buttonStyle(PlainButtonStyle()).accentColor(.clear)
            
            
            
        }
    }
}

struct SearchSortView_Previews: PreviewProvider {
    static var previews: some View {
        SearchSortView(sortViewIsExpanded: false)
    }
}
